using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using CustomLitJson;
using System.Threading.Tasks;

public static class ProjectBuildPiplineCommandLine
{
    public static async void Build()
    {
        Debug.Log("[ProjectBuildPiplineCommandLine] started");
        var schema = "";
        var env = "";
        var switchToPlatform = "";
        var paramTemplate = "";
        var paramJd = new JsonData();
        CommandLineUtil.ReadOptions((arg, value) =>
        {
            if(arg == "--schema")
            {
                schema = value;
            }
            else if(arg == "--env")
            {
                env = value;
            }
            else if(arg == "--switchToPlatform")
            {
                switchToPlatform = value;
            }
            else if(arg == "--paramTemplate")
            {
                paramTemplate = value;
            }
            var key = arg.TrimStart('-');
            paramJd[key] = value;
        });

        if (schema == "")
        {
            throw new Exception($"[Project Build Pipline CommandLine] need option '--schema'");
        }




        //Debug.Log("[ProjectBuildPiplineCommandLine] schame param: " + paramJson);
        //var paramNameList = ProjectBuildPipline.GetSchemaParamNameList(schema);
        //var paramDic = CreateParamDic(paramNameList);
        var schemaType = ProjectBuildPipline.FindSchemaTypeByName(schema);
        var paramType = ProjectBuildPipline.GetParamTypeOfSchema(schemaType);

        JsonData finalParamJd = new JsonData();
        finalParamJd.SetJsonType(JsonType.Object);
        if (!string.IsNullOrEmpty(paramTemplate))
        {
            var templateParamObj = ProjectBuildPipline.ReadParamTempalte(paramType, paramTemplate);
            var templateParamJson = JsonMapper.Instance.ToJson(templateParamObj);
            var templateParamJd = JsonMapper.Instance.ToObject(templateParamJson);
            var dic = templateParamJd.ToDictionary();
            foreach (var kv in dic)
            {
                var key = kv.Key;
                var jdValue = kv.Value;
                finalParamJd[key] = jdValue;
            }
        }
        {
            var dic = paramJd.ToDictionary();
            foreach (var kv in dic)
            {
                var key = kv.Key;
                var jdValue = kv.Value;
                finalParamJd[key] = jdValue;
            }
        }

        var paramJson = JsonMapper.Instance.ToJson(finalParamJd);
        var paramObj = JsonMapper.Instance.ToObject(paramType, paramJson);

        {
            // log param
            var newjson = JsonMapper.Instance.ToJson(paramObj);
            newjson = JsonUtil.Buitify(newjson);
            Debug.Log("param: " + newjson);
        }

        await ProjectBuildPipline.Build(schema, paramObj, env, switchToPlatform);
        Debug.Log("[ProjectBuildPiplineCommandLine] complete");
    }

    public static Dictionary<string, string> CreateParamDic(List<string> paramNameList)
    {
        var paramDic = new Dictionary<string, string>();
        var cmdParamDic = new Dictionary<string, string>();
        CommandLineUtil.ReadOptions((arg, value) =>
        {
            var key = arg.TrimStart('-');
            cmdParamDic[key] = value;
        });
        foreach(var param in paramNameList)
        {
            if(cmdParamDic.ContainsKey(param))
            {
                var value = cmdParamDic[param];
                paramDic[param] = value;
            }
            else
            {
                throw new Exception($"[Project Build Pipline] schema need param `{param}`");
            }
        }
        return paramDic;
    }
}
